package com.ftw.commons.network

import android.annotation.SuppressLint
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers


/**
 * 描    述：
 * 作    者：fantianwen
 * 时    间：2019/4/16  5:03 PM
 */

object HttpUtils {

    @SuppressLint("CheckResult")
    fun <T> toSubscribe(
        ob: Observable<T>,
        activity: RxAppCompatActivity,
        subscriber: HttpSubscriber<T>
    ) {
        //数据预处理
        ob
            .compose { upstream ->
                upstream
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
            }
            .compose(activity.bindToLifecycle())
            .subscribe(subscriber)

    }


    @SuppressLint("CheckResult")
    fun <T> toSubscribe(
        ob: Observable<T>,
        subscriber: HttpSubscriber<T>
    ) {
        //数据预处理
        ob
            .compose { upstream ->
                upstream
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
            }
            .subscribe(subscriber)

    }
}
